const innerAudioContext = wx.createInnerAudioContext();
const app = getApp();

Page({
  data: {
    full_loading: true,

    ganzhi: {},
    scrollTop: 0,  // 页面滚动高度

    taiji_seed: 0,
    taiji_index: 0,  // 0 ~ 7 循环
    taiji_list: [],

    season_seed: 0,
    season: {},
    season_index: 0,
    season_list: [],

    center_coord: [0, 0],  // 中心点坐标
    half_eight_width: 0,  // 八卦半宽
    BaguaList: [],

    list64: [],
    list_64_in: false,

    instr: '',
    open_instr: false,

    preload_list: []  // 预加载图片列表
  },
  onLoad() {
    this.setData({ ganzhi: app.get_ganzhi() });

    this.getTaijiList(() => {
      setTimeout(() => {
        this.setData({ full_loading: false});
      }, 1500);
    });
    // 太极
    this.data.taiji_seed = setInterval(() => {
      if (this.data.taiji_index !== 7) {
        this.data.taiji_index++;
      } else {
        this.data.taiji_index = 0;
      }
    }, 250);

    // 四季
    this.getJieqiList(() => {
      this.setData({ season: this.data.season_list[this.data.season_index] });
      this.data.season_index++;
      this.data.season_seed = setInterval(() => {
        this.setData({ season: this.data.season_list[this.data.season_index] });
        if (this.data.season_index !== 7) {
          this.data.season_index++;
        } else {
          this.data.season_index = 0;
        }
      }, 4000);

      // 预加载
      let img_arr = [];
      for (let i = 0; i < this.data.season_list.length; i++) {
        img_arr.push(this.data.season_list[i].pic);
      }
      this.setData({ preload_list: this.data.preload_list.concat(img_arr) });
    });

    // 八宫
    this.getBaguaList();

    // 主题
    this.getXiangxianList(() => {
      this._list64_huan();

      setInterval(() => {
        this._list64_huan();
      }, 6000);

      // 预加载
      let img_arr = [];
      for (let i = 0; i < this.data.list64.length; i++) {
        if (this.data.list64[i].list) {
          for (let j = 0; j < this.data.list64[i].list.length; j++) {
            img_arr.push(this.data.list64[i].list[j].pic);
          }
        }
      }
      this.setData({ preload_list: this.data.preload_list.concat(img_arr) });
    });

    // 使用指南
    this.instruction();
  },
  onReady() {
    setTimeout(() => {
      const query = wx.createSelectorQuery();
      query.select('.eight-gua').boundingClientRect((res) => {
        this.data.half_eight_width = res.width / 2;
        this.data.center_coord[0] = res.left + res.width / 2;
        this.data.center_coord[1] = res.top + this.data.scrollTop + res.height / 2;
      }).exec();
    }, 500);
  },
  onPageScroll(e) {
    this.data.scrollTop = e.scrollTop;
  },
  _list64_huan() {
    for (let i = 0; i < this.data.list64.length; i++) {
      let sort = [7, 2, 3, 0, 4, 8, 5, 6, 1];
      if (i !== 4) {
        setTimeout(() => {
          this.setData(
            {
              ['list64[' + sort[i] + '].now_index']: this.data.list64[sort[i]].now_index !== this.data.list64[sort[i]].list.length - 1 ? this.data.list64[sort[i]].now_index + 1 : 0
            });
        }, i < 4 ? (i + 1) * 750 : i * 750);
      }
    }
  },
  // 获取太极文章
  getTaijiList(complete) {
    app.ajax('index/getTaijiList', null, res => {
      this.data.taiji_list = res;
    }, null, () => {
      complete();
    });
  },
  // 获取节气文章
  getJieqiList(callback) {
    app.ajax('index/getJieqiList', null, res => {
      app.format_img_arr(res);
      this.setData({ season_list: res });

      if (callback) {
        callback();
      }
    });
  },
  // 获取八卦文章
  getBaguaList() {
    app.ajax('index/getGongList', null, res => {
      res.sort((a, b) => {
        return a.id_sort < b.id_sort ? -1 : 1;
      });

      app.format_img_arr(res);

      this.setData({ BaguaList: res })
    });
  },
  // 获取64象限文章
  getXiangxianList(complete) {
    app.ajax('index/zhutiList', null, res => {
      let list64 = [];
      for (let i = 0; i < res.length; i++) {
        app.format_img_arr(res[i]);
        list64.push({
          now_index: 0,
          in: true,
          list: res[i]
        });
      }

      list64.splice(4, 0, {});

      this.setData({ list64: list64 });
    }, null, () => {
      if (complete) {
        complete();
      }
    });
  },
  // 去详情页
  to_detail(e) {
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case '1':  // 太极
        wx.navigateTo({ url: '/pages/taiji-detail/taiji-detail?id=' + this.data.taiji_list[this.data.taiji_index].id });
        break;
      case '2':  // 八季和八卦
        let distance = app.distance(e.detail.x, this.data.center_coord[0], e.detail.y, this.data.center_coord[1]);
        if (distance / this.data.half_eight_width > 0.6) {
          // 点的是八卦
          let angle = app.angle(e.detail.x, this.data.center_coord[0], e.detail.y, this.data.center_coord[1]);

          let index = Math.floor((angle + 22.5) / 45);
          index = index === 8 ? 0 : index;

          wx.navigateTo({ url: '/pages/gong-detail/gong-detail?id=' + this.data.BaguaList[index].id });
        } else {
          // 点的是八季
          wx.navigateTo({ url: '/pages/season-detail/season-detail?id=' + this.data.season.id });
        }
        break;
      case '3':
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({ url: '/pages/gong-zhuti-detail/gong-zhuti-detail?id=' + id });
        break;
    }
  },
  // 使用指南
  instruction() {
    app.ajax('index/instruction', null, (res) => {
      this.setData({ instr: res });
    });
  },
  toggle_instruction() {
    this.setData({ open_instr: !this.data.open_instr });
  },
  to_taiji() {
    wx.navigateTo({ url: '/pages/taiji/taiji' });
  },
  to_jingdian() {
    wx.navigateTo({ url: '/pages/jingdian/jingdian' });
  },
  start() {
    console.log('touchstart');
  },
  end() {
    console.log('touchend');
  },
  // 分享
  onShareAppMessage() {
    wx.showShareMenu();
    return { path: app.share_path() };
  }
});